package com.sun.hunk.struts.action;

import com.sun.hunk.hibernate.DAOFactory;
import com.sun.hunk.hibernate.Property;
import com.sun.hunk.hibernate.QueryOperator;
import com.sun.hunk.hibernate.dao.PurchaseDAO;
import com.sun.hunk.hibernate.domain.ItemCharge;
import com.sun.hunk.hibernate.domain.Purchase;
import com.sun.hunk.hibernate.domain.impl.ItemChargeImpl;
import com.sun.hunk.struts.HunkDispatchAction;
import com.sun.hunk.struts.form.PurchaseForm;
import com.sun.hunk.util.CommonUtil;
import com.sun.hunk.util.DateUtil;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author sunil
 */
public class PurchaseAction extends HunkDispatchAction {

    public ActionForward action(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        PurchaseForm purchaseForm = (PurchaseForm) form;
        if ("Save".equalsIgnoreCase(purchaseForm.getButton())) {
            return save(mapping, form, request, response);
        } else {
            return search(mapping, form, request, response);
        }
    }

    public ActionForward search(ActionMapping mapping, ActionForm f, HttpServletRequest request, HttpServletResponse response) throws Exception {
        PurchaseForm form = (PurchaseForm) f;
        PurchaseDAO dao = new PurchaseDAO();
        List<Property> criteria = new ArrayList<Property>();
        if (CommonUtil.isNotEmpty(form.getItem())) {
            criteria.add(Property.getInstance("item.description", form.getItem(), QueryOperator.LIKE));
        }
        if (CommonUtil.isNotEmpty(form.getStartDate())) {
            Date startDate = DateUtil.parseToDate(form.getStartDate(), DateUtil.Format.MM_DD_YYYY);
            criteria.add(Property.getInstance("purchaseDate", startDate, QueryOperator.GREATER_THAN_EQUAL_TO));
        }
        List<Purchase> list = dao.find(criteria);
        request.setAttribute("purchaseList", list);
        return mapping.findForward("search");
    }

    public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        PurchaseForm purchaseForm = (PurchaseForm) form;
        Purchase purchase = purchaseForm.getPurchase();
        DAOFactory.getPurchaseDAO().saveOrUpdate(purchase);
        setMessage(request, "Save Successfully !");
        return mapping.findForward(SUCCESS);
    }

    public ActionForward edit(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        PurchaseForm purchaseForm = (PurchaseForm) form;
        Purchase purchase = DAOFactory.getPurchaseDAO().findById(purchaseForm.getId());
        purchaseForm.setPurchase(purchase);
        return mapping.findForward(SUCCESS);
    }

    public ActionForward addCharge(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        PurchaseForm purchaseForm = (PurchaseForm) form;
        ItemCharge charge = new ItemChargeImpl();
        charge.setPurchase(purchaseForm.getPurchase());
        purchaseForm.getPurchase().getCharges().add(charge);
        return mapping.findForward(SUCCESS);
    }

    public ActionForward removeCharge(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        int index = Integer.parseInt(request.getParameter(INDEX));
        PurchaseForm purchaseForm = (PurchaseForm) form;
        purchaseForm.getPurchase().getCharges().remove(index);
        return mapping.findForward(SUCCESS);
    }
    public ActionForward clear(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String page = request.getParameter("page");
        if(page == null){
            page = SUCCESS;
        }
        request.getSession().removeAttribute("purchaseForm");
        return mapping.findForward(page);
    }

    public boolean validate(PurchaseForm purchaseForm, HttpServletRequest request) {

        return true;
    }

    public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
        try {
            PurchaseForm purchaseForm = (PurchaseForm) form;
            setMessage(request, "Purchase deleted !");
        } catch (Exception e) {
            setError(request, "oops ! something went wrong.");
        }
        return mapping.findForward("message");
    }
}
